package com.decent.text;

import com.alibaba.fastjson.annotation.JSONField;
import com.decent.entity.FbTest;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.*;

/**
 * @author 王雪杰
 * @date 2021/7/28 19:11
 * @description
 */
public class OtherPeopleTest {
    public static void main(String[] args) throws Exception {
        FbTest fbTest = new FbTest();
        fbTest.setDay1(1);
        fbTest.setDay2(2);
        fbTest.setDay3(3);
        FbTest fbTest1 = new FbTest();
        fbTest1.setDay1(1);
        fbTest1.setDay2(1);
        fbTest1.setDay3(3);
        ArrayList<FbTest> integers = new ArrayList<>();


        integers.add(fbTest);
        integers.add(fbTest1);
        String filed = "name";
        String count = "count";
        integers.forEach(fbTest2 -> {
            Class<FbTest> clazz = FbTest.class;
            Field[] declaredFields = clazz.getDeclaredFields();
            for (Field declaredField : declaredFields) {
                declaredField.setAccessible(true);
                if (!declaredField.getName().equals(filed)
                        && !declaredField.getName().equals(count)) {
                    Field countField = null;
                    try {
                        countField = clazz.getDeclaredField(count);
                        countField.setAccessible(true);
                        if (countField.get(fbTest2) == null) {
                            countField.set(fbTest2, 0);
                        }
                    } catch (NoSuchFieldException e) {
                        System.out.print("sss\n");
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                    try {
                        assert countField != null;
                        Object declaredFieldValue = declaredField.get(fbTest2);
                        countField.set(fbTest2,
                                (Integer) (declaredFieldValue == null ? 0 : declaredFieldValue)
                                        + (Integer) countField.get(fbTest2));
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }


        });
        System.out.println(integers);
    }

    /**
     * 对象转map
     *
     * @param o 对象
     * @return Map<String, Object>
     * @throws IllegalAccessException
     */
    private Map<String, Object> object2Map(Object o) throws IllegalAccessException {
        if (o == null) {
            return null;
        }
        Map<String, Object> map = new HashMap<>();
        Class<?> clazz = o.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            Object fieldValue = field.get(o);
            if (Objects.nonNull(fieldValue)) {
                String fileName = field.getName();
                Annotation[] annotations = field.getAnnotations();
                for (Annotation annotation : annotations) {
                    if (annotation instanceof JSONField) {
                        fileName = ((JSONField) annotation).name();
                        break;
                    }
                    if (annotation instanceof JsonProperty) {
                        fileName = ((JsonProperty) annotation).value();
                        break;
                    }
                }
                map.put(fileName, fieldValue);
            }
        }
        return map;
    }

}
